home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr34 / blowpas.zip / BLOWF_D.ASM next >
Assembly Source File  |  1994-12-31  |  4KB  |  164 lines

  1. ; Assembly implementation of BlowFish D function.
  2. ;
  3. ; Original algoritm by Bruce Schneier
  4. ; First assembly implementation by John Lots and Walter van Holst
  5. ; Assembly rewrite by Jeroen Pluimers
  6. ;
  7. ; This code is hereby donated to the public domain
  8. ;
  9. ; Version history of rewritten code:
  10. ;   19941230 1.00.00 jwp - implemented assembly code from scratch
  11. ;
  12. ; Original Pascal code:
  13. ;   Type
  14. ;     TPArray = array[1..18] of Longint;
  15. ;     TSBox = array[1..4,0..255] of Longint;
  16. ;   procedure BlowDecrypt(var paramXl,paramXr:LongInt; PArray:TPArray; SBox: TSBox);
  17. ;   var
  18. ;     Xl,Xr, Temp: Longint;
  19. ;     i: Byte;
  20. ;   begin
  21. ;     Xl := paramXl;
  22. ;     Xr := paramXr;
  23. ;     for i := 18 downto 3 do begin
  24. ;       Xl:=Xl XOR P[i];
  25. ;       Xr:=F(Xl,S) XOR Xr;
  26. ;       Temp:=Xl;
  27. ;       Xl:=Xr;
  28. ;       Xr:=Temp;
  29. ;     end;
  30. ;     Temp:=Xl;
  31. ;     Xl:=Xr;
  32. ;     Xr:=Temp;
  33. ;     Xr:=Xr XOR P[2];
  34. ;     Xl:=Xl XOR P[1];
  35. ;     paramXl := Xl;
  36. ;     paramXr := Xr;
  37. ;   end;
  38.  
  39. Ideal
  40. P386
  41. Model   TPascal
  42.  
  43. CodeSeg
  44.  
  45.   Struc TPArray
  46.     contents      dd      18 dup(?)
  47.   EndS
  48.  
  49.   TPArraySize equ Size TPArray
  50.  
  51.   Struc TSBox
  52.     box1          dd      256 dup(?)
  53.     box2          dd      256 dup(?)
  54.     box3          dd      256 dup(?)
  55.     box4          dd      256 dup(?)
  56.   EndS
  57.  
  58.   Proc    BlowDecrypt Far Xl: DWord, Xr: DWord, PArray: DWord, SBox: DWord
  59.   Public  BlowDecrypt
  60.   ; NOTE:
  61.   ;  - This routine is FAR
  62.  
  63.   Extrn   F: Near
  64.  
  65.   ; stack frame is automatically generated
  66.  
  67.           push    ds
  68.  
  69.           ; load the passed data
  70.           lds     si,  [Xl]
  71.           mov     ebx, [ds:si]
  72.           lds     si,  [Xr]
  73.           mov     edx, [ds:si]
  74.           lds     si,  [PArray]
  75.           les     di,  [SBox]
  76.  
  77.   ; ds:si = PArray[i]
  78.   ; es:di = SBox
  79.   ; B = Xl
  80.   ; D = Xr
  81.  
  82.   ; note: each time we call F, we must save ds:si, es:di, B and D
  83.  
  84.           mov     cx,  16
  85.           ; Reverse direction, so point to end of array and do STD
  86.           add     si,  TPArraySize-4
  87.  
  88.   @@Loop:
  89.  
  90.           ; Xl := Xl XOR PArray[i];
  91.  
  92.           pushf   ; be nice to software that expects the D flag to be saved
  93.           std
  94.           lodsd   ; Get PArray[i] into eax and increment si by 4
  95.           popf
  96.           xor     ebx, eax
  97.  
  98.           ; Xr := F(Xl,SBox) XOR Xr;
  99.  
  100.           push    cx
  101.           push    ebx   ; new Xl
  102.           push    edx   ; old Xr
  103.           push    ds
  104.           push    si
  105.           push    es
  106.           push    di
  107.  
  108.           push    ebx
  109.           push    es
  110.           push    di
  111.           call    F
  112.  
  113.           ; get dx:ax into edx
  114.           shl     edx, 16
  115.           mov     dx,  ax
  116.  
  117.           pop     di
  118.           pop     es
  119.           pop     si
  120.           pop     ds
  121.           pop     eax     ; old Xr
  122.           pop     ebx     ; new Xl
  123.           pop     cx
  124.  
  125.           xor     edx, eax   ; new Xr
  126.  
  127.           ; Swap new Xl and new Xr
  128.  
  129.           xchg    ebx, edx
  130.  
  131.           loop    @@Loop
  132.  
  133.           ; Swap new Xl and new Xr back
  134.  
  135.           xchg    ebx, edx
  136.  
  137.           ; Xr:=Xr XOR PArray[2];
  138.           ; Xl:=Xl XOR PArray[1];
  139.  
  140.           pushf   ; be nice to software that expects the D flag to be saved
  141.           std
  142.           lodsd   ; get PArray[2]
  143.           xor     edx, eax
  144.           lodsd   ; get PArray[1]
  145.           xor     ebx, eax
  146.           popf
  147.  
  148.           lds     si,  [Xl]
  149.           mov     [ds:si], ebx
  150.           lds     si,  [Xr]
  151.           mov     [ds:si], edx
  152.  
  153.           pop     ds
  154.  
  155.           ret
  156.  
  157.   ; stack cleanup is automatically generated
  158.  
  159.   EndP ; BlowDecrypt
  160.  
  161.  
  162. EndS  ; CodeSeg
  163.  
  164. End   ; Source